import 'package:convergence_media/di/di.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/article_request.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/article_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/category_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/comment_more_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/comment_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/comment_vote_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/content_comment_request.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/content_comment_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/content_content_id_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/content_fav_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/content_forward_request.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/content_forward_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/content_like_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/content_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/contribution_get_contribution_list_request.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/contribution_get_contribution_list_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/contribution_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/expose_list_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/expose_request.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/expose_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/expose_type_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/file_upimage_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/links_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/login_mobile_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_edit_request.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_edit_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_expose_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_follow_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_get_fanslist_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_get_followlist_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_get_my_follow_count_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_get_user_follow_count_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_info_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_invitation_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_is_follow_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_location_request.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_location_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/my_my_fav_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/navigation_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/position_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/position_splash_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/search_hotkeyword_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/search_request.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/search_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/special_response.dart';
import 'package:convergence_media/features/api/dtos/news_csavs_dtos/special_unique_id_response.dart';
import 'package:dio/dio.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'news_csavs_api.g.dart';

@riverpod
NewsCsavsApi newsCsavsApi(NewsCsavsApiRef ref) {
  return NewsCsavsApi(dio: ref.watch(newsDioProvider));
}

/// {@template news_csavs_api}
/// 创视新闻接口
/// {@endtemplate}
class NewsCsavsApi {
  /// {@macro news_csavs_api}
  const NewsCsavsApi({required Dio dio}) : _dio = dio;

  final Dio _dio;

  /// /category/
  FutureOr<CategoryResponse> postCategory() async {
    final uri = Uri(path: '/category/');

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return CategoryResponse.fromJson(response.data!);
  }

  /// /content/
  FutureOr<ContentResponse> postContent(
    String categoryId,
    String page,
    String t,
  ) async {
    final uri = Uri(path: '/content/');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({'c': categoryId, 'p': page, 't': t}),
    );

    return ContentResponse.fromJson(response.data!);
  }

  /// /position/
  FutureOr<PositionSplashResponse> postPositionSplash(
    String p,
    String id,
  ) async {
    final uri = Uri(
      path: '/position/',
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'p': p,
        'id': id,
      }),
    );

    return PositionSplashResponse.fromJson(response.data!);
  }

  /// /navigation/
  FutureOr<NavigationResponse> postNavigation() async {
    final uri = Uri(path: '/navigation/');

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return NavigationResponse.fromJson(response.data!);
  }

  /// /login/mobile
  FutureOr<LoginMobileResponse> postLoginMobile(String loginToken) async {
    final uri = Uri(path: '/login/mobile');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'logintoken': loginToken,
      }),
    );

    return LoginMobileResponse.fromJson(response.data!);
  }

  /// /content/${contentId}
  FutureOr<ContentContentIdResponse> postContentContentId(
    String contentId,
  ) async {
    final uri = Uri(path: '/content/$contentId');

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return ContentContentIdResponse.fromJson(response.data!);
  }

  /// /content/comment
  FutureOr<ContentCommentResponse> postContentComment(
    ContentCommentRequest contentCommentRequest,
  ) async {
    final uri = Uri(path: '/content/comment');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(contentCommentRequest.toJson()),
    );

    return ContentCommentResponse.fromJson(response.data!);
  }

  /// /article/
  FutureOr<ArticleResponse> postArticle(
    ArticleRequest articleRequest,
  ) async {
    final uri = Uri(path: '/article/');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(articleRequest.toJson()),
    );

    return ArticleResponse.fromJson(response.data!);
  }

  /// /comment/
  FutureOr<CommentResponse> postComment(String contentId, String page) async {
    final uri = Uri(path: '/comment/');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'contentId': contentId,
        'pg': page,
      }),
    );

    return CommentResponse.fromJson(response.data!);
  }

  /// /expose/
  FutureOr<ExposeResponse> postExpose(
    ExposeRequest exposeRequest,
  ) async {
    final uri = Uri(path: '/expose/');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(exposeRequest.toJson()),
    );

    return ExposeResponse.fromJson(response.data!);
  }

  /// /record/
  // FutureOr<RecordResponse> postRecord() async {
  //   final uri = Uri(
  //     path: '/record/',
  //     queryParameters: {

  //     },
  //   );

  //   final response = await _dio.postUri<Map<String, dynamic>>(
  //     uri,
  //     data: FormData.fromMap({

  //     }),
  //   );

  //   return RecordResponse.fromJson(response.data!);
  // }

  /// /my/info
  FutureOr<MyInfoResponse> postMyInfo(String token) async {
    final uri = Uri(path: '/my/info');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'token': token,
      }),
    );

    return MyInfoResponse.fromJson(response.data!);
  }

  /// /special
  FutureOr<SpecialResponse> postSpecial() async {
    final uri = Uri(
      path: '/special',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({}),
    );

    return SpecialResponse.fromJson(response.data!);
  }

  /// /special/{$uniqueId}
  FutureOr<SpecialUniqueIdResponse> postSpecialUniqueId(String uniqueId) async {
    final uri = Uri(path: '/special/{$uniqueId}');

    final response = await _dio.postUri<Map<String, dynamic>>(uri);

    return SpecialUniqueIdResponse.fromJson(response.data!);
  }

  /// /my/expose
  FutureOr<MyExposeResponse> postMyExpose({
    required String token,
    required String page,
  }) async {
    final uri = Uri(path: '/my/expose');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'token': token,
        'pg': page,
      }),
    );

    return MyExposeResponse.fromJson(response.data!);
  }

  /// /my/location
  FutureOr<MyLocationResponse> postMyLocation(
    MyLocationRequest myLocationRequest,
  ) async {
    final uri = Uri(path: '/my/location');

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(myLocationRequest.toJson()),
    );

    return MyLocationResponse.fromJson(response.data!);
  }

  /// /search/hotkeyword
  FutureOr<SearchHotkeywordResponse> postSearchHotkeyword() async {
    final uri = Uri(
      path: '/search/hotkeyword',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({}),
    );

    return SearchHotkeywordResponse.fromJson(response.data!);
  }

  /// /search
  FutureOr<SearchResponse> postSearch(SearchRequest searchRequest) async {
    final uri = Uri(
      path: '/search',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(searchRequest.toJson()),
    );

    return SearchResponse.fromJson(response.data!);
  }

  /// /expose/type
  FutureOr<ExposeTypeResponse> postExposeType() async {
    final uri = Uri(
      path: '/expose/type',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({}),
    );

    return ExposeTypeResponse.fromJson(response.data!);
  }

  /// /expose/list
  FutureOr<ExposeListResponse> postExposeList(String page) async {
    final uri = Uri(
      path: '/expose/list',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'pg': page,
      }),
    );

    return ExposeListResponse.fromJson(response.data!);
  }

  /// /content/forward
  FutureOr<ContentForwardResponse> postContentForward(
    ContentForwardRequest contentForwardRequest,
  ) async {
    final uri = Uri(
      path: '/content/forward',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(contentForwardRequest.toJson()),
    );

    return ContentForwardResponse.fromJson(response.data!);
  }

  /// /comment/more
  FutureOr<CommentMoreResponse> postCommentMore({
    required String id,
    required String contentId,
  }) async {
    final uri = Uri(
      path: '/comment/more',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'id': id,
        'content_id': contentId,
      }),
    );

    return CommentMoreResponse.fromJson(response.data!);
  }

  /// /my/invitation
  FutureOr<MyInvitationResponse> postMyInvitation({
    required String token,
    required String invitation,
  }) async {
    final uri = Uri(
      path: '/my/invitation',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'token': token,
        'invitation': invitation,
      }),
    );

    return MyInvitationResponse.fromJson(response.data!);
  }

  /// /file/upimage
  FutureOr<FileUpimageResponse> postFileUpimage({
    required String filePath,
    required String token,
  }) async {
    final uri = Uri(
      path: '/file/upimage',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'image': await MultipartFile.fromFile(filePath),
        'token': token,
      }),
    );

    return FileUpimageResponse.fromJson(response.data!);
  }

  /// /content/fav
  FutureOr<ContentFavResponse> postContentFav(
    String contentId,
    String token,
  ) async {
    final uri = Uri(
      path: '/content/fav',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'contentId': contentId,
        'token': token,
      }),
    );

    return ContentFavResponse.fromJson(response.data!);
  }

  /// /content/like
  FutureOr<ContentLikeResponse> postContentLike(
    String contentId,
    String token,
  ) async {
    final uri = Uri(
      path: '/content/like',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'contentId': contentId,
        'token': token,
      }),
    );

    return ContentLikeResponse.fromJson(response.data!);
  }

  /// /comment/vote
  FutureOr<CommentVoteResponse> postCommentVote(
    String id,
    String token,
  ) async {
    final uri = Uri(
      path: '/comment/vote',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'id': id,
        'token': token,
      }),
    );

    return CommentVoteResponse.fromJson(response.data!);
  }

  /// /position
  FutureOr<PositionResponse> postPosition(
    String page,
    String outTime,
    String id,
    String pageSize,
  ) async {
    final uri = Uri(
      path: '/position',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'p': page,
        't': outTime,
        'id': id,
        'num': pageSize,
      }),
    );

    return PositionResponse.fromJson(response.data!);
  }

  /// /links
  FutureOr<LinksResponse> postLinks() async {
    final uri = Uri(
      path: '/links',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({}),
    );

    return LinksResponse.fromJson(response.data!);
  }

  /// /contribution/
  FutureOr<ContributionResponse> postContribution() async {
    final uri = Uri(
      path: '/contribution/',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({}),
    );

    return ContributionResponse.fromJson(response.data!);
  }

  /// /Contribution/getContributionList
  FutureOr<ContributionGetContributionListResponse>
      postContributionGetContributionList(
    ContributionGetContributionListRequest
        contributionGetContributionListRequest,
  ) async {
    final uri = Uri(
      path: '/Contribution/getContributionList',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(contributionGetContributionListRequest.toJson()),
    );

    return ContributionGetContributionListResponse.fromJson(response.data!);
  }

  /// /my/edit
  FutureOr<MyEditResponse> postMyEdit(MyEditRequest myEditRequest) async {
    final uri = Uri(
      path: '/my/edit',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap(myEditRequest.toJson()),
    );

    return MyEditResponse.fromJson(response.data!);
  }

  /// /my/follow
  FutureOr<MyFollowResponse> postMyFollow(String token, String toUserId) async {
    final uri = Uri(
      path: '/my/follow',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'token': token,
        'to_user_id': toUserId,
      }),
    );

    return MyFollowResponse.fromJson(response.data!);
  }

  /// /my/getFollowlist
  FutureOr<MyGetFollowlistResponse> postMyGetFollowlist() async {
    final uri = Uri(
      path: '/my/getFollowlist',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({}),
    );

    return MyGetFollowlistResponse.fromJson(response.data!);
  }

  /// /login/updateImid
  // FutureOr<LoginUpdateImidResponse> postLoginUpdateImid() async {
  //   final uri = Uri(
  //     path: '/login/updateImid',
  //     queryParameters: {

  //     },
  //   );

  //   final response = await _dio.postUri<Map<String, dynamic>>(
  //     uri,
  //     data: FormData.fromMap({

  //     }),
  //   );

  //   return LoginUpdateImidResponse.fromJson(response.data!);
  // }

  /// /my/is_follow
  FutureOr<MyIsFollowResponse> postMyIsFollow(
    String toUid,
    String token,
  ) async {
    final uri = Uri(
      path: '/my/is_follow',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'to_uid': toUid,
        'token': token,
      }),
    );

    return MyIsFollowResponse.fromJson(response.data!);
  }

  /// /my/getUserFollowCount
  FutureOr<MyGetUserFollowCountResponse> postMyGetUserFollowCount(
    String toUid,
    String token,
  ) async {
    final uri = Uri(
      path: '/my/getUserFollowCount',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'to_uid': toUid,
        'token': token,
      }),
    );

    return MyGetUserFollowCountResponse.fromJson(response.data!);
  }

  /// /my/getMyFollowCount
  FutureOr<MyGetMyFollowCountResponse> postMyGetMyFollowCount(
    String token,
  ) async {
    final uri = Uri(
      path: '/my/getMyFollowCount',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'token': token,
      }),
    );

    return MyGetMyFollowCountResponse.fromJson(response.data!);
  }

  /// /my/getFanslist
  FutureOr<MyGetFanslistResponse> postMyGetFanslist(String token) async {
    final uri = Uri(
      path: '/my/getFanslist',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'token': token,
      }),
    );

    return MyGetFanslistResponse.fromJson(response.data!);
  }

  // TODO(lilua): 数据混乱
  /// /my/myFav
  FutureOr<MyMyFavResponse> postMyMyFav(
    String token,
    String page,
    String pageSize,
  ) async {
    final uri = Uri(
      path: '/my/myFav',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'token': token,
        'pg': page,
        'pg_size': pageSize,
      }),
    );

    return MyMyFavResponse.fromJson(response.data!);
  }
}
